package org.stone.project.common.utils;

import cn.hutool.extra.spring.SpringUtil;
import org.stone.project.common.pojo.exception.BuzException;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

public class ValidUtil {
    /**
     * 参数校验
     * @param obj
     * @param <T>
     */
    public static <T> void checkParam(T obj) {
        Validator valid = SpringUtil.getBean(Validator.class);
        Set<ConstraintViolation<T>> violations = valid.validate(obj);
        if (!violations.isEmpty()) {
            // 处理验证错误
            StringBuilder errorMessages = new StringBuilder();
            for (ConstraintViolation<T> violation : violations) {
                errorMessages.append(violation.getMessage()).append("; ");
            }
            throw new BuzException("参数校验失败: " + errorMessages.toString());
        }
    }
}
